package com.dm.contriller;


import com.alibaba.fastjson.JSONObject;
import com.dm.client.dm_user.RestDmUserClient;
import com.dm.common.Constants;
import com.dm.common.EmptyUtils;
import com.dm.common.RedisUtils;
import com.dm.dto.Dto;
import com.dm.dto.DtoUtil;
import com.dm.exception.ErrorCode;
import com.dm.pojo.dm_user.DmUser;
import com.dm.service.use_token.TokenService;
import com.dm.service.user_login.LoginDmUser;
import com.dm.vo.DmUserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user/api")
public class UserRegisterController {

    @Resource
    private LoginDmUser loginDmUser;
    @Resource
    private RestDmUserClient restDmUserClient;
    @Resource
    private RedisUtils redisUtils;


    /**
     * 验证手机和邮箱号是否可用接口
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/p/checkEmail", method = RequestMethod.POST)
    public Dto checkEmail(@RequestParam(value = "phone", required = true) String phone) throws Exception {
        //=====1：判断是手机号还是邮箱
        Map<String,Object> map=new HashMap<>();
        Integer count=null;
        if(loginDmUser.validPhon(phone)){
        //=====2：手机
            //=====1：是否注册返回指定信息
            map.put("phone",phone);
            count=restDmUserClient.getDmUserCountByMap(map);
            if(EmptyUtils.isEmpty(count)||count==0){
                return  DtoUtil.returnSuccess(ErrorCode.PHONE_NO.getErrorMessage());
            }else if(EmptyUtils.isNotEmpty(count)||count!=0){
                return  DtoUtil.returnFail(ErrorCode.PHONE_REGISTER.getErrorMessage(),ErrorCode.PHONE_REGISTER.getErrorCode());
            }

        }else if (loginDmUser.validEmail(phone)){
        //=====3：邮箱
            //=====1：是否注册返回指定信息
            map.put("phone",phone);
            count=restDmUserClient.getDmUserCountByMap(map);
            if(EmptyUtils.isEmpty(count)||count==0){
                return  DtoUtil.returnSuccess(ErrorCode.EMAIL_NO.getErrorMessage());
            }else if(EmptyUtils.isNotEmpty(count)||count!=0){
                return  DtoUtil.returnFail(ErrorCode.EMAIL_REGISTER.getErrorMessage(),ErrorCode.EMAIL_REGISTER.getErrorCode());
            }
        }
        //=====4：格式不正确
        return  DtoUtil.returnFail(ErrorCode.EMAIL_OR_PHONE_ERROR.getErrorMessage(),ErrorCode.EMAIL_OR_PHONE_ERROR.getErrorCode());


    }

    /**
     * 获取注册验证码接口
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/p/code", method = RequestMethod.POST)
    public Dto code(@RequestParam(value = "phone", required = true) String phone) throws Exception {

        try {
            //=====1：判断是手机号还是邮箱
            String ma=null;
            //判断验证码是否过期
            if(redisUtils.exist(Constants.LoginAndRegisterCode.register+phone)){
                return DtoUtil.returnFail(ErrorCode.FREEZE_TIME_NO.getErrorCode(),ErrorCode.FREEZE_TIME_NO.getErrorMessage());
            }
            if(loginDmUser.validPhon(phone)){
                //=====2：手机
                ma=loginDmUser.sendPhon(Constants.LoginAndRegisterCode.register,phone);

            }else if (loginDmUser.validEmail(phone)){
                //=====3：邮箱
                ma=loginDmUser.sendMail(Constants.LoginAndRegisterCode.register,phone);
            }else{

                return DtoUtil.returnFail(ErrorCode.EMAIL_OR_PHONE_ERROR.getErrorMessage(),ErrorCode.EMAIL_OR_PHONE_ERROR.getErrorCode());
            }

            return DtoUtil.returnSuccess(ErrorCode.VERIFICATION_SEND.getErrorMessage(),ma);
        }catch (Exception e){
            //错误
            return DtoUtil.returnFail(ErrorCode.VERIFICATION_SEND_ERROR.getErrorMessage(),ErrorCode.VERIFICATION_SEND_ERROR.getErrorCode());
        }
    }

    /**
     * 使用邮箱/手机号注册接口
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/p/register", method = RequestMethod.POST)
    public Dto register(
            @RequestParam(value = "phone", required = true) String phone,
            @RequestParam(value = "vcode", required = true) String vcode,
            @RequestParam(value = "password", required = true) String password) throws Exception {
            loginDmUser.addRegisterUser(phone,password);
            return DtoUtil.returnSuccess(ErrorCode.REDISTER_SUCCEES.getErrorMessage());
    }



}
